

function dragSort(_ref) {
    var id = _ref.id, // 需拖动元素父级ID
        drag = _ref.drag, // 拖动的元素class（需  draggable="true" 其中  链接和图像默认是可拖动的）
        times = _ref.times; // 拖动动画时长

    if (!times) {
        times = 1000
    };
    this.id = id;
    this.drag = drag;
    this.times = times; // 拖动动画时长
    this.isSongKai = false
    this.init();
};

// 给实例对象添加公共属性和方法
dragSort.prototype = {

    init: function init() {

        for (i = 0; i < this.id.length; i++) {
            // console.log(this.id[i]);
            var node = document.getElementById(this.id[i]);
            // console.log(node);


            var dragClass = this.drag, _this = this;
            // console.log("dragClass-->"+dragClass);
            var draging = null;
            //使用事件委托，将li的事件委托给ul
            node.ondragstart = function (event) {
                // console.log("start");
                draging = event.target;
            }

            var naniList = []
            var startIndex = -1, endIndex = -1
            var isCanAssign = true//是否可以赋值，起始索引赋值闸
            var isok = false

            node.ondragover = function (event) {

                // console.log("onDrop over");
                event.preventDefault();
                var target = event.target;
                // console.log(target);
                if (!_this.hasClassName(target, dragClass)) {
                    // console.log("!!!!!!!!!!!_this.hasClassName");
                    target = target.parentNode
                }

                // 进行属性判断，是否覆盖到移动的标签上
                if (_this.hasClassName(target, dragClass)) {
                    // console.log("_this.hasClassName");
                    if (target && target.animated) {
                        // console.log("是否覆盖到移动的标签上:");
                        return;
                    }
                    this.isSongKai = false
                    isok = false
                    var targetIndex = _this.index(target), dragingIndex = _this.index(draging),
                        nextNode = '', animateObj = [];

                    if (isCanAssign) {
                        startIndex = targetIndex
                        isCanAssign = false
                    }
                    // console.log(node.id);
                    if (targetIndex != dragingIndex) { // 拖拽框位置判断
                        endIndex = targetIndex
                        if (targetIndex > dragingIndex) { // 向后拖拽
                            var preIndex = _this.index(draging),
                                nextIndex = _this.index(target),
                                existingnode = target.nextElementSibling;
                        } else { // 向前拖拽
                            var preIndex = _this.index(target),
                                nextIndex = _this.index(draging),
                                existingnode = target;
                        }
                        for (var i = 0; i < nextIndex - preIndex + 1; i++) {
                            nextNode = nextNode == "" ? draging : (targetIndex > dragingIndex ? nextNode.nextElementSibling : nextNode.previousElementSibling);
                            animateObj.push([nextNode.getBoundingClientRect(), nextNode])
                        }
                        target.parentNode.insertBefore(draging, existingnode);
                        // console.log("animateObj.length" + animateObj.length)
                        var length = animateObj.length - 1
                        for (var i = 0; i < animateObj.length; i++) {
                            // console.log("animateObj.遍历--" + i)

                            if (length == i) {
                                isok = true
                            }
                            _this.animate(animateObj[i][0], animateObj[i][1], isok, startIndex, endIndex);
                        }
                        naniList = animateObj
                        // console.log("draging-->"+draging+" ; existingnode-->"+existingnode)
                        // console.log("startIndex-->" + startIndex + " ; endIndex-->" + endIndex)

                    }
                }
            }

            //拖动结束
            node.ondragend = function (event) {
                isCanAssign = true
                this.isSongKai = true

                // console.log("拖动结束--》isok--" + isok)
                // console.log("拖动结束--》this.isSongKai--" + this.isSongKai)

                // if (isok && this.isSongKai) {
                //     console.log("拖动结束--"+ this.isSongKai);
                //     var type = event.path[1].id
                //     songListener(type, startIndex, endIndex)
                // }

                var type = event.path[1].id
                console.log(event.path[1])
                songListener(type, startIndex, endIndex)
            }
        }
    },
    // 判断是否包含某个class
    hasClassName: function hasClassName(obj, name) {
        var tmpName = obj.className;
        var tmpReg = new RegExp(name, 'g');
        return tmpReg.test(tmpName) ? true : false;
    },
    //获取元素在父元素中的index
    index: function _index(el) {
        var index = 0;
        if (!el || !el.parentNode) {
            return -1;
        }
        while (el && (el = el.previousElementSibling)) {
            index++;
        }
        return index;
    },
    // 元素移动
    animate: function _animate(prevRect, target, isok, startIndex, endIndex) {
        // console.log(target.animated)
        var ms = this.times;
        var _this = this;
        if (ms) {
            var currentRect = target.getBoundingClientRect();
            if (prevRect.nodeType === 1) {
                prevRect = prevRect.getBoundingClientRect();
            }
            _this.css(target, 'transition', 'none');
            _this.css(target, 'transform', 'translate3d(' +
                (prevRect.left - currentRect.left) + 'px,' +
                (prevRect.top - currentRect.top) + 'px,0)'
            );
            target.offsetWidth; // 触发重绘
            _this.css(target, 'transition', 'all ' + ms + 'ms');
            _this.css(target, 'transform', 'translate3d(0,0,0)');

            clearTimeout(target.animated);
            console.log("this.isSongKai" + this.isSongKai)
            target.animated = setTimeout(function () {
                _this.css(target, 'transition', '');
                _this.css(target, 'transform', '');
                target.animated = false;

                // console.log("异步--》isok--" + isok)
                // console.log("异步--》this.isSongKai--" + _this.isSongKai)
                // if (isok && this.isSongKai) {
                //     console.log("异步--》" + startIndex + "--" + endIndex)
                //     // console.log(target.id.slice(0,-1))
                //     // var type = target.id.slice(0,-1)
                //     songListener(type, startIndex, endIndex)
                //     // location.reload();
                // }
            }, ms);
        }
    },
    // 给元素添加style
    css: function _css(el, prop, val) {
        var style = el && el.style;

        if (style) {
            if (val === void 0) {
                if (document.defaultView && document.defaultView.getComputedStyle) {
                    val = document.defaultView.getComputedStyle(el, '');
                } else if (el.currentStyle) {
                    val = el.currentStyle;
                }
                return prop === void 0 ? val : val[prop];
            } else {
                if (!(prop in style)) {
                    prop = '-webkit-' + prop;
                }
                style[prop] = val + (typeof val === 'string' ? '' : 'px');
            }
        }
    }
};
